<script setup lang="ts">

import TextEditor from "@/components/TextEditor.vue";
import {onMounted, reactive, ref} from "vue";
import {ElMessage} from "element-plus";
import {useProblemStore} from "@/stores/problem.ts";

const problemForm = reactive({
  problemId: '',
  title: '',
  difficulty: 1,
  content: '',
})

const problemStore = useProblemStore()

onMounted(async function () {
  problemForm.problemId = problemStore.problemId
  problemForm.title = problemStore.title
  problemForm.difficulty = problemStore.difficulty
  problemForm.content = problemStore.content
})

const textEditor = ref<InstanceType<typeof TextEditor> | null>(null);

// 调用删除图片的方法
const removeUnusedImages = async () => {
  if (textEditor.value) {
    await textEditor.value.removeUnusedImages();
  }
};

async function saveProblem() {
  problemForm.content = textEditor.value ? textEditor.value.valueHtml.trim() : ''
  if (problemForm.title.trim() === '') {
    ElMessage({
      message: '题目标题不能为空',
      type: 'warning',
    })
  } else if (problemForm.difficulty === undefined || problemForm.difficulty < 1 || problemForm.difficulty > 3) {
    ElMessage({
      message: '题目难度不能为空',
      type: 'warning',
    })
  } else if (problemForm.content === '' || problemForm.content === '<p><br></p>') {
    ElMessage({
      message: '题目内容不能为空',
      type: 'warning',
    })
  } else {
    try {
      await removeUnusedImages()
      if (problemForm.problemId === '') {
        await problemStore.saveProblem(problemForm)
        problemForm.problemId = problemStore.problemId
      } else {
        await problemStore.updateProblem(problemForm)
      }
      ElMessage({
        message: '保存成功',
        type: 'success',
      })
    } catch (e:any) {
      ElMessage({
        message: e,
        type: 'error',
      })
    }
  }
}


</script>

<template>
  <div class="container">
    <div class="left">
      <div class="title">
        <div class="left">
          标题:
        </div>
        <div class="right">
          <el-input
              v-model="problemForm.title"
              style="width: 750px"
              maxlength="255"
              placeholder="请输入题目标题"
              show-word-limit
              type="text"
          />
        </div>
      </div>
      <div class="difficulty">
        <div class="left">难度:</div>
        <div class="right">
          <el-select v-model="problemForm.difficulty" placeholder="选择难度" style="width: 200px;">
            <el-option label="简单" :value="1"/>
            <el-option label="中等" :value="2"/>
            <el-option label="困难" :value="3"/>
          </el-select>
        </div>
      </div>
      <TextEditor ref="textEditor" style="width: 1000px; height: 600px"/>
      <div class="saveButton">
        <el-button type="primary" @click="saveProblem()">
          保存
          <el-icon class="el-icon--right">
            <Upload/>
          </el-icon>
        </el-button>
      </div>
    </div>
    <div class="right">
      <!--      <el-image style="width: 100px;" src="@/assets/助手.webp" fit="none" />-->
<!--      <img style="width: 300px" src="@/assets/助手.webp" alt="助手">-->
    </div>
  </div>
</template>

<style scoped lang="scss">
.container {
  display: flex;
  .left {
    .title {
      display: flex;
      align-items: center;

      .left {
        margin-right: 20px;
      }
    }

    .difficulty {
      display: flex;
      align-items: center;
      margin-top: 10px;
      margin-bottom: 10px;

      .left {
        margin-right: 20px;
      }
    }

    .saveButton {
      width: 800px;
      margin-top: 20px;
      display: flex;
      justify-content: center;
    }
  }
}
</style>